<?php
/**
 * Upload
 * @author Erko Risthein <erko@risthein.ee>
 * @copyright Copyright (C) 2009 Erko Risthein
 * @link http://erko.risthein.ee/decode/
 * @license http://www.gnu.org/licenses/ GNU General Public License
 * @package decode_explorer
 */
class Upload {
	const MAX_FILENAME_LENGTH = 260;
	private $save_path;
	private $input_file;
	private $php_error;
	private $tmp_name;
	private $file_name;
	private $error_msg;
	private $php_errors = array(
		UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
		UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
		UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.',
		UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
		UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.',
		UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.',
		UPLOAD_ERR_EXTENSION => 'File upload stopped by extension.',
	);
	
	/**
	 * @param string $save_path where to save the uploaded file
	 * @param string $var_name the input tag's name parameter used in the HTML form
	 */
	function __construct($save_path, $var_name) {
		$this->save_path = $save_path;
		$this->input_file = $_FILES[$var_name];
		$this->php_error = $_FILES[$var_name]['error'];
		$this->tmp_name = $_FILES[$var_name]['tmp_name'];
		$this->file_name = $_FILES[$var_name]['name'];
	}
	
	/**
	 * Checks if the upload exeeds the POST maximum size or the upload maximum filesize
	 * @return boolean
	 */
	private function exeeds_max_size() {
		$post_max_size = php_ini_to_bytes(ini_get('post_max_size'));
		$upload_max_filesize = php_ini_to_bytes(ini_get('upload_max_filesize'));
		if ((int) $_SERVER['CONTENT_LENGTH'] > $post_max_size && (int) $_SERVER['CONTENT_LENGTH'] > $upload_max_filesize) { //exeeds the POST max size or the upload max filesize
			return true;
		}
		return false;
	}
	
	/**
	 * Converts the php.ini values to bytes
	 * @param string $value php.ini value
	 * @return int value in bytes
	 */
	private function php_ini_to_bytes($value) {
		$value = trim($value);
		$unit = strtoupper(substr($value, -1));
		switch($unit) {
			case 'G':
				$value *= 1024;
			case 'M':
				$value *= 1024;
			case 'K':
				$value *= 1024;
		}
		return $value;
	}
	
	/**
	 * Validates the upload
	 * @return boolean
	 */
	private function is_valid_upload() {
		if (!isset($input_file)) {
			//no upload found
			return false;
		} else if ($this->php_error != UPLOAD_ERR_OK) {
			//handle error
			return false;
		} else if (!isset($this->tmp_name)) {
			//no tmp_name set
			return false;
		} else if (!isset($this->file_name) {
			//no name
			return false;
		} if (file_exists($this->save_path . $this->file_name)) {
			//file already exists
			return false;
		}
		return true;
	}
	
	/**
	 * Moves the uploaded file from the temporary directory to the saving path
	 * @return boolean
	 */
	private function move_file() {
		if (!@move_uploaded_file($this->tmp_name, $this->save_path . $this->file_name)) {
			//file could not be saved
			return false;
		}
		//file successfully uploaded
		return true;
	}
}
?>